<template>
	<view class="page">
		<view class="code-get" v-if="!isSetting">
			<view class="phone">
				<text>请输入<text>{{tphone}}</text>短信验证码</text>
			</view>
			<view class="code">
				<input type="tel" v-model="code" maxlength="6" placeholder="请输入短信验证码">
				<text @click="sendcode">获取验证码</text>
			</view>
			<view class="btn" @click="checkcode">
				<text>下一步</text>
			</view>
		</view>
		<view class="setting-password" v-else>
			<view class="inpu-password">
				<text>设置密码</text>
				<input type="password" v-model="password" placeholder="请输入要设置的密码" />
				<text class="iconfont icon-eye-on icon"></text>
			</view>
			<view class="btn">
				<text @click="updatePwd">确认</text>
			</view>
		</view>
	</view>
</template>

<script>
	import user from '../api/user.js'
	export default {
		data() {
			return {
				isSetting: false,
				phone:'',
				tphone:'',
				type:"3",
				code:"",
				password:"",
			};
		},
		methods:{
			checkcode(){ 
				 user.checkcode({
				 	  	 phone: this.phone,
				 	  	 type: this.type,
						 code: Number(this.code)
						 
				 }).then(res=>{
				 	  				 if(res.code==1)
									 {
										 this.isSetting = true 
									 }else{
										  uni.showToast({
				 	  				        title: res.message,
				 	  				        icon: 'success',
				 	  				        duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
				 	  				       });
							 
									 }
				 	  				 
				 });
			},
			updatePwd(){
				user.userUpdate({
					  			pwd: this.password, 
				}).then(res=>{
					  
					              uni.showToast({
								       title: res.message,
								       icon: 'success',
								       duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
								      });
									  setTimeout(() => {
									       uni.navigateBack("pages/AccountSecurity/AccountSecurity");
									   }, 1500);
									  
				});	
			},
			sendcode(){
				user.sendCode({
					  				  phone: this.phone,
					  				  type: this.type, 
				}).then(res=>{ 
					  				  uni.showToast({
					  				        title: res.message,
					  				        icon: 'success',
					  				        duration: 1500 // 提示框显示的时间，单位为毫秒，默认 1500
					  				       });
				}); 
				console.log("发送验证码")
			},
		},
		onShow() {
		     const token = uni.getStorageSync('token'); 
		     if (token) { 
		         const userInfo = uni.getStorageSync('usernfo'); 
		         if (userInfo) {
					 this.phone=userInfo.phone;			
		             var aa = userInfo.phone; 
					 const start = aa.substring(0, 3);
					 const end = aa.substring(7);
					         // 中间部分用星号代替
					         const maskedPart = '****';
					         this.tphone = start + maskedPart + end;
		         }
		     } else {
				 this.phone="未绑定";
		         
		     }
		},
	}
</script>

<style scoped lang="scss">
	@import 'ChangePassword.scss';
</style>
